<?php

/**
 * 呼入报表
 */
class CallinreportformsController extends Controller_Base
{
    /**
     * 首页 - 列表
     */
    public function indexAction()
    {
        $post_date = $this->getRequest()->getQuery('post_date', date("Y-m-d", time()));
        $dataSelect = $this->getRequest()->getQuery('dataSelect', 'date');
        $mediationOrganId = trim($this->getRequest()->getQuery('mediationOrganId', 0));

        if ($dataSelect == 'date') {
            $begin_date_int = strtotime($post_date);
            $end_date_int = $begin_date_int + 86400;
        } elseif ($dataSelect == '周报' || $dataSelect == 'daterange') {
            $arr = explode(" 到", $post_date);
            $begin_date_int = strtotime($arr[0]);
            $end_date_int = strtotime($arr[1]) + 86400;
        } elseif ($dataSelect == 'month') {
            $begin_date_int = strtotime($post_date);
            $end_date_int = strtotime(date("Y-m", strtotime("+1 month", $begin_date_int)));
        } elseif ($dataSelect == 'year') {
            $begin_date_int = strtotime($post_date.'-01'.'-01');
            $end_date_int = strtotime(date("Y-1-1", strtotime("+1 year", $begin_date_int)));
        }

        // where
        $search = [];
        $search['where'] = $this->getWhere();
        if ($begin_date_int && $end_date_int) {
            array_push($search['where'], "countTime >= {$begin_date_int}  and countTime<$end_date_int");
        }
        $pageQuery['mediationOrganId'] = $mediationOrganId;

        $search['order'] = 'id desc';
        $listArr = [];
        $tmp = [
            'callCount' => 0,
            'callTime' => 0,
            'avgCallTime' => 0,
            'longCallTime' => 0,
            'receivedNum' => 0,
            'missedCallsNum' => 0,
            'callCompletionRate' => 0,
            'mediationOrganId' => 0,
            'countTime' => $post_date,
        ];

        //汇总
        $countlist = [
            'callCount' => 0,
            'callTime' => 0,
            'avgCallTime' => 0,
            'longCallTime' => 0,
            'receivedNum' => 0,
            'missedCallsNum' => 0,
            'callCompletionRate' => 0,
        ];
        $list = Module_Mediate_CallinReportforms::getInstance()->getAllBySearch([], $search);
        if (!empty($list)) {

            foreach ($list as $k => $v) {
                if(!isset($listArr[$v['workerId']])){
                    $listArr[$v['workerId']] = $tmp;
                }
                $listArr[$v['workerId']]['workerId'] = $v['workerId'];
                $listArr[$v['workerId']]['callCount'] += $v['callCount'];
                $listArr[$v['workerId']]['callTime'] += $v['callTime'];
                if ($listArr[$v['workerId']]['longCallTime'] < $v['longCallTime']) {
                    $listArr[$v['workerId']]['longCallTime'] = $v['longCallTime'];
                }
                $listArr[$v['workerId']]['receivedNum'] += $v['receivedNum'];
                $listArr[$v['workerId']]['missedCallsNum'] += $v['missedCallsNum'];

                $listArr[$v['workerId']]['mediationOrganId'] = $v['mediationOrganId'];
                $listArr[$v['workerId']]['mediatorName'] = $v['mediatorName'];
            }

            foreach ($listArr as $k => $v) {
                $listArr[$k]['avgCallTime'] = $v['receivedNum'] > 0 ? sprintf("%.2f", $v['callTime'] / $v['receivedNum']) : 0;
                $listArr[$k]['callCompletionRate'] = $v['receivedNum'] + $v['missedCallsNum'] > 0 ? sprintf("%.4f", $v['receivedNum'] / ($v['receivedNum'] + $v['missedCallsNum'])) : 0;

                $countlist['callCount'] += $v['callCount'];
                $countlist['callTime'] += $v['callTime'];
                if ($v['longCallTime'] > $countlist['longCallTime']) {
                    $countlist['longCallTime'] = $v['longCallTime'];
                }
                $countlist['receivedNum'] += $v['receivedNum'];
                $countlist['missedCallsNum'] += $v['missedCallsNum'];

            }
            $countlist['avgCallTime'] = $countlist['receivedNum'] > 0 ? sprintf("%.2f", $countlist['callTime'] / $countlist['receivedNum']) : 0;
            $countlist['callCompletionRate'] = $countlist['receivedNum'] + $countlist['missedCallsNum'] > 0 ? sprintf("%.4f", $countlist['receivedNum'] / ($countlist['receivedNum'] + $countlist['missedCallsNum'])) : 0;
        }

        //assign
        $query['dataSelect'] = $dataSelect;
        $query['post_date'] = $post_date;
        $query['mediationOrganId'] = $mediationOrganId;

        //赋值调解部门
        $this->getMediationOrgan();
        SmartyAdapter::assign('countlist', $countlist);
        SmartyAdapter::assign('page_query', empty($query) ? '' : http_build_query($query));
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('list', $listArr);
        SmartyAdapter::$templateName = 'callinreportforms/index';
    }


}
